美文网首页
Java快速搭建微服务

Java快速搭建微服务

作者: 学习微站 | 来源:发表于2022-11-17 22:56 被阅读0次

源代码:https://gitee.com/riccxie/spring-cloud-demo2

一、创建新工程


下一步

二、在这个项目New Module 添加EurekaServer 子项目



有的时候start.spring.io会超时建不了,可以等一会或者手动建,用阿里url效果最佳 https://start.aliyun.com/
网上方法
初始化成大概这样

3、application.properties,这里我替换成application.yml


server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false 
    register-with-eureka: false 
    service-url:
      
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

4、加了controller

@RestController
public class indexController {
	@GetMapping("/")
	public String index() {
		return "index";
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

5/输入http://localhost:8761/,它可能没识别到访问Eureka控制台,本来要进入访问Eureka控制台的

修改一下, 把IndexController全部注释掉
启动类加上注解@EnableEurekaServer
这样就能进入这样的界面
服务正常启动

然后还需要生产者、消费者

三、建生产者module

按照同样的 创建时需要勾选 Spring Cloud Discover–> Eureka Discover Client 和 Spring Web 的依赖。

启动类

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClientApplication.class, args);
	}

	@RequestMapping("sayHello")
	public String sayHello(String param) {
		return "Hello " + param;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

application.yml

server:
  port: 8765

spring:
  application:
    name: eureka-client

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

刷新后

四、消费者

按照同样的 创建时需要勾选 Spring Cloud Discover–> Eureka Discover Client 和 Spring Web 的依赖。

启动类

package com.example.eureka_consumer;

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;

/**
 * 在启动类中添加@EnableDiscoveryClient表明标注类是消费者,加入restTemplate来消费相关的服务
 */
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaConsumerApplication.class, args);
	}

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

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

controller

package com.example.eureka_consumer.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author samxie
 * @version 1.0
 * @date 2022-2-13 14:28
 */
@RestController
@Slf4j
public class DemoController {
	@Autowired
	RestTemplate restTemplate;

	@RequestMapping("/greet")
	public String sayHello(@RequestParam String name) {
		return restTemplate.getForObject("http://EUREKA-CLIENT/sayHello?param=" + name, String.class);
	}

}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

配置文件application.yml

server:
  port: 8763

spring:
  application:
    name: eureka-consumer

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

结果看到生成
5.打开浏览器输入localhost:{server.port}/path 进行服务调用,

这里我用 http://localhost:8763/greet?name=eureka100 ,可以看到请求正确返回,正确调用了服务提供者。

https://www.cnblogs.com/binyue/p/12079356.html

本文使用 文章同步助手 同步

相关文章

网友评论

      本文标题:Java快速搭建微服务

      本文链接:https://www.haomeiwen.com/subject/yqgqtdtx.html