微服務入門案例

Spring Boot 2.2.0
依賴Spring web與EureKa Server
maven項目裏創建eureka名Spring Boot 項目

@EnableEurekaServer//開啓註冊中心服務

@SpringBootApplication
@EnableEurekaServer//開啓註冊中心服務
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);}}

application.properties

##註冊中心服務名字
spring.application.name=eureka 
server.port=1111
#是否將自己註冊到EureKa中,默認true
eureka.client.register-with-eureka=false
#是否Eurka中心服務中獲取註冊信息默認true
eureka.client.fotch-registry=false
#註冊中心把自己服務信息註冊到上面
eureka.client.service-url.defaultZone=http://localhost:1111/eureka

啓動http://localhost:1111/看到自己的服務信息

服務端
maven項目裏創建名providerSpring Boot 項目
依賴是Spring web與Eureka Discovery Client

application.properties

#服務端名字
spring.application.name=provider
#服務端信息名字和端口註冊到註冊中心
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
server.port=2000

啓動二個服務:http://localhost:1111/服務提供者註冊到註冊中心

@RestController
public class HelloController {
    @Value("${server.port}")
    Integer port;   //獲取端口
    @GetMapping("/hello")
    public String hello(){
        return "服務端hello註冊信息"+port;
    }

客服端
maven項目裏創建名consumerSpring Boot 項目
依賴是Spring web與Eureka Discovery Client

application.properties

#客服端
spring.application.name=consumer
server.port=3000
#客服端服務註冊到註冊中心
eureka.client.service-url.defaultZone=http://localhost:1111/eureka

UseHelloController消費信息

@RestController
public class UseHelloController {
    @Autowired
    DiscoveryClient discoveryClient; //發現客服端

    @GetMapping("/hello")
    public void hello()throws IOException {
        List<ServiceInstance> list = discoveryClient.getInstances("provider");//客服端根據服務端名到註冊中心列表獲取詳細信息
        ServiceInstance serviceInstance = list.get(0); //鏈表中根據size取服務實例
        String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/hello";//服務完整信息
        HttpURLConnection con = null; //初始化
        URL url1 = new URL(url);  //服務端http://localhost:2000/hello
        con = (HttpURLConnection)url1.openConnection(); //打開服務連接
        if (con.getResponseCode() == 200){ //獲取響應碼200比較
            BufferedReader buff = new BufferedReader(new InputStreamReader(con.getInputStream()));//字節流輸入到緩存
            String s = buff.readLine(); //緩存中讀到字符串信息(服務端hello註冊2000)
            System.out.println(s); //打印(服務端hello註冊2000)
            buff.close();
        }

三個服務啓動註冊到註冊中心http://localhost:1111/
啓動客服端http://localhost:3000/hello
總體來說與之前那個http調用相似

客服端負載均衡實現
Buid打包一個jar包啓動

java -jar provider-0.0.1-SNAPSHOT.jar --server.post=2001

啓動二個提供者服務端
在這裏插入圖片描述

 Integer count=0;
 ServiceInstance serviceInstance = list.get((count++)%list.size()); //取餘是0.1

客服端訪問http://localhost:3000/hello

服務端hello註冊2001
服務端hello註冊2000
服務端hello註冊2001

基於RestTemplate實現客服端消費(第二種)

  @Bean
    RestTemplate restTemplate(){
      return new RestTemplate();
    }
   @Autowired
    RestTemplate restTemplate;

    @GetMapping("/hello2")
    public void hello2()throws IOException {
        List<ServiceInstance> list = discoveryClient.getInstances("provider");
        ServiceInstance serviceInstance = list.get((count++) % list.size()); //長度2  count 1或0
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/hello";
        String s = restTemplate.getForObject(url,String.class);
        System.out.println(s); 
    }
 }

訪問http://localhost:3000/hello2

發佈了66 篇原創文章 · 獲贊 68 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章