【前面的話】書接上文,本文的某些知識依賴個人上一篇文章:SpringCloud之Eureka,若是沒有看過能夠先移步去看一下。另外在微服務架構中,業務都會被拆分紅一個個獨立的服務,服務與服務的通信是基於http restful的。Spring cloud有兩種服務調用方式,一種是ribbon+restTemplate,另外一種是feign。在這一篇文章首先講解下基於ribbon+rest。java
ribbon是一個負載均衡客戶端,能夠很好的控制htt和tcp的一些行爲。Feign默認集成了ribbon。git
ribbon 已經默認實現了這些配置bean:github
IClientConfig ribbonClientConfig: DefaultClientConfigImpl IRule ribbonRule: ZoneAvoidanceRule IPing ribbonPing: NoOpPing ServerList ribbonServerList: ConfigurationBasedServerList ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
<parent> <artifactId>lovincloud</artifactId> <groupId>com.eelve.lovincloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>lovin-ribbon-client</artifactId> <packaging>jar</packaging> <name>ribbonclient</name> <version>0.0.1</version> <description>ribbon的client</description> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
server: port: 8805 # 服務端口號 spring: application: name: lovinribbonclient # 服務名稱 security: basic: enabled: true user: name: lovin password: ${REGISTRY_SERVER_PASSWORD:lovin} eureka: client: serviceUrl: defaultZone: http://lovin:lovin[@localhost](https://my.oschina.net/u/570656):8881/eureka/ # 註冊到的eureka服務地址
package com.eelve.lovin.cofig; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; /** * [@ClassName](https://my.oschina.net/u/3112573) SecurityConfig * @Description TDO * [@Author](https://my.oschina.net/arthor) zhao.zhilue * [@Date](https://my.oschina.net/u/2504391) 2019/8/16 14:12 * @Version 1.0 **/ @Configuration @Order(0) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().permitAll() .and().csrf().disable(); } }
package com.eelve.lovin; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; /** * @ClassName LovinRibbonClientApplication * @Description TDO * @Author zhao.zhilue * @Date 2019/8/15 16:59 * @Version 1.0 **/ @SpringBootApplication @EnableDiscoveryClient public class LovinRibbonClientApplication { public static void main(String[] args) { SpringApplication.run(LovinRibbonClientApplication.class,args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
package com.eelve.lovin.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; /** * @ClassName HelloService * @Description TDO * @Author zhao.zhilue * @Date 2019/8/15 17:02 * @Version 1.0 **/ @Service public class HelloService { @Autowired RestTemplate restTemplate; public String getHello() { //這裏的**lovineurkaclient**是我上一篇文章新建的eureka客戶端的名稱 return restTemplate.getForObject("http://lovineurkaclient/hello",String.class); } }
package com.eelve.lovin.controller; import com.eelve.lovin.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @ClassName HelloController * @Description TDO * @Author zhao.zhilue * @Date 2019/8/15 17:05 * @Version 1.0 **/ @RestController public class HelloController { @Autowired HelloService helloService; @RequestMapping("hello") public String hello(){ return helloService.getHello(); } }