一:springCloud服務發現者,服務消費者(方誌朋《史上最簡單的 SpringCloud 教程》專欄讀後感)
阿新 • • 發佈:2018-12-16
- 註冊服務中心 new–>project–>spring Initializr—>(next)…—>Dependencies(Cloud Discovery Eureka Server) 在入口類處加註解@EnableEurekaServer表明該伺服器是一個服務註冊中心
@SpringBootApplication //服務註冊中心 @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
配置application.yml
#伺服器埠號 server: port: 8761 #向服務中心註冊 eureka: client: service-url: defaultZone: http://localhost:8761 #eureka.client.register-with-eureka:false(是否向自身註冊),eureka.client.fetch-register:false表明該伺服器是個eureka server register-with-eureka: false fetch-registry: false spring: application: name: eureka
- 服務提供者 (建立過程同上) 在入口類加入註解@EnableDiscoveryClient,@EnableEurekaClient表明這是一個eureka client
@SpringBootApplication @EnableDiscoveryClient @EnableEurekaClient //注意:如果單獨建立controller類,要放在該入口類的子包下,否則掃描不到 @RestController public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("/hi") public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) { return "來自"+port+"的"+name+"人類,歡迎來到我們的時間"; } }
server:
port: 8762
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
hostname: clientName
spring:
application:
name: eureka-client
在pom.xml中加jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
配置EurekaClientApplication下的小三角里的Edit Configurations–>single instance only(去掉對勾) ,改application.yml裡的埠號,改為8763,啟動
- 服務消費者
在pom.xml配置加入以下配置
<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-web</artifactId>
</dependency>
<!--ribbon:負載均衡客戶端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
@SpringBootApplication
@EnableEurekaClient
//向服務中心註冊
@EnableDiscoveryClient
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
//向程式的ioc注入一個bean
@Bean
//表明這個restTemplated開啟負載均衡
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
建立Service類
@Service
public class HelloService {
@Autowired
private RestTemplate restTemplate;
public String hiService(String name){
return restTemplate.getForObject("http://EUREKA-CLIENT/hi?name="+name,String.class);
}
}
建立Controller類
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping(value = "/hi")
public String hi(@RequestParam String name){
return helloService.hiService(name);
}
}
分別開啟服務註冊中心,服務提供者(8762,8763),服務消費者,訪問http://localhost:8764/hi?name=zy,不斷重新整理,就會發現頁面交替出現,
來自8762的zy人類,歡迎來到我們的時間
來自8763的zy人類,歡迎來到我們的時間
說明已經達到了負載均衡