栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springcloud入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springcloud入门

目录

一,创建服务注册发现中心工程:lj-cloud-server-eureka

 二,创建服务提供者:lj-cloud-provider1

三,创建消费者:lj-cloud-sonsumer


参考:https://blog.csdn.net/weixin_38023579/article/details/81328524

 

一,创建服务注册发现中心工程:lj-cloud-server-eureka

 

 删掉application.properties,新建application.yml

spring:
  application:
    name: server-eureka

#  服务注册中心的端口
server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    #    表示是否将自己注册到 Eureka Server
    register-with-eureka: false
    #    表示是否从 Eureka Server 获取注册信息
    fetch-registry: false
    #    设置与 Eureka Server 交互的地址,查询服务和注册服务都需要依赖这个地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类添加注解:@EnableEurekaServer

package com.example.ljcloudservereureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class LjCloudServerEurekaApplication {

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

        System.out.println("---服务监控访问地址"+"http://localhost:8761");
    }

}

 启动项目,启动成功后访问地址:http://localhost:8761

 二,创建服务提供者:lj-cloud-provider1

 

注:用controller要导入spring web

# 应用端口
server:
  port: 7901
spring:
  application:
    # 应用名称
    name: provider-user

# eureka 配置
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

logging:
  level:
    root: INFO

 

package com.example.ljcloudprovider1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient  //表明这是一个eureka客户端
public class LjCloudProvider1Application {

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

        System.out.println("---ljcloudprovider1 服务监控访问地址"+"http://localhost:7901");
    }

}

 创建UserController,用来提供测试用的服务

 

package com.example.ljcloudprovider1.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/sayHello")
    public String sayhello(){
        return "I`m provider 1 ,Hello consumer!";
    }
    @RequestMapping("/sayHi")
    public String sayHi(){
        return "I`m provider 1 ,Hi consumer!";
    }
}

 右键运行服务提供端(在此之前需先启动server-eureka)

 启动后,刷新 http://localhost:8761/

 

 成功!

三,创建消费者:lj-cloud-sonsumer

 

# 应用端口
server:
  port: 8002
spring:
  application:
    # 应用名称
    name: lj-cloud-consumer

# eureka 配置
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

logging:
  level:
    root: INFO

package com.example.ljcloudconsumer.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient("provider-user")
public interface UserClient {

    @RequestMapping(value="/user/sayHello",method=RequestMethod.GET)
    public String sayHello();

    @RequestMapping(value="/user/sayHi",method= RequestMethod.GET)
    public String sayHi();

}

 

package com.example.ljcloudconsumer.controller;

import com.example.ljcloudconsumer.service.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private UserClient feignClient;

    @GetMapping("/hello")
    public String hello(){
        return feignClient.sayHello();
    }

    @GetMapping("/hi")
    public String hi(){
        return feignClient.sayHi();
    }


}

package com.example.ljcloudconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient //表明这是一个eureka客户端
@EnableFeignClients //开启feign
public class LjCloudConsumerApplication {

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

        System.out.println("---ljcloudconsumer 服务监控访问地址"+"http://localhost:8002");
    }

}

 右键启动。访问:http://localhost:8002/hi

后来想测试zuul网关和hystrix熔断器,但是导包一直报错,就没再继续了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/305840.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号